'use strict';

angular.module('fbdApp.templateModule')

.controller('ruleGroupCtrl', ['$scope', 'modalService', '$location', function(
  $scope, modalService, $location) {
  // $scope.rules = [];
  // $scope.rules = shareData.rules;
  $scope.hasSonTemplate = true;

  $scope.goSonTemplate = function() {
    // console.log(shareData);

    if ($scope.hasSonTemplate) {
      $scope.hasSonTemplate = false;
      // alert('test shareData');
      // var father = {};
      // father.rules = shareData.rules;
      // father.tempName = shareData.tempName;
      // father.realFileName = shareData.realFileName;
      // father.fileName = shareData.fileName;
      // father.articleList = shareData.articleList;
      // shareData.sonTemplate = father;
    }
    $scope.$emit('isSonTemplate', true);
    $location.path('/steps_1');
    $location.replace();
    // alert('goSonTemplate');
    // console.log(shareData.sonTemplate);
    // console.log(shareData);
  };
  // [{
  //   name: 'ab',
  //   type: '全文匹配',
  //   beginText: 'beginab',
  //   endText: 'endab'

  // }, {
  //   name: 'abc',
  //   type: '全文匹配',
  //   beginText: 'begin111abc',
  //   endText: 'end222222abc'

  // },
  // {
  //   name: 'abcd',
  //   type: '全文匹配',
  //   beginText: 'begin111abcd',
  //   endText: 'end222222abcd'

  // }];

  // $scope.$watch('rules', function(nval, oval, scope) {
  //   // alert($scope.rules);
  //   // $scope.$apply();
  //   var par = document.getElementsByTagName('td');
  //   angular.forEach(par, function(data, index, arr) {
  //     var text = angular.element(data).text();
  //     // console.log(text);
  //     if (text == 1) {
  //       // alert('1');
  //       angular.element(data).text('匹配开头');
  //     }
  //     if (text == 2) {
  //       // alert('2');
  //       angular.element(data).text('匹配开头结尾');
  //     }
  //     if (text == 3) {
  //       // alert('3');
  //       angular.element(data).text('匹配全文');
  //     }
  //   });
  // }, true);

  $scope.index = ''; //选中规则在规则中的下标
  $scope.isSelected = ''; //是否有选中?
  $scope.selectedItem = ''; //选中的规则modify

  $scope.modify = function() {
    var modalConfig = {
      controller: 'steps_5',
      resolve: {
        selected: function() {
          return angular.copy($scope.selectedItem)
        }
      }
    };

    var modalInfo = {
      closeButtonText: '取消',
      actionButtonText: '确定',
      headerText: '添加规则',
      bodyText: '<new-rule test-type="string" rule-data="ruleData" article-list="articleList" saved-rule="savedRule" modify="true"></new-rule>'
    };
    // console.log(string);

    modalService.showModal(modalConfig, modalInfo).result.then(function(
      res) {
      // $scope.testResult = result;
      $scope.rules.splice($scope.index, 1, res);
    }, function(reason) {
      // $scope.testResult = reason;
    });
  };

  $scope.select = function(item) {

    // angular.forEach($scope.rules, function(data, index) {
    //   console.log(data.name + '====')
    // });
    // alert(item);
    angular.forEach($scope.rules, function(data, index, array) {
      if (data === item) {
        // alert(index);
        $scope.index = index;
      }

    });
    $scope.isSelected = true;
    $scope.selectedItem = item;
  };

  $scope.add = function() {
    var modalConfig = {
      controller: 'steps_5',
      resolve: {
        selected: function() {
          return {}
        }
      }
    };

    var modalInfo = {
      closeButtonText: '取消',
      actionButtonText: '确定',
      headerText: '添加规则',
      bodyText: '<new-rule test-type="string" article-list="articleList" saved-rule="savedRule"></new-rule>'
    };

    modalService.showModal(modalConfig, modalInfo).result.then(function(
      res) {
      $scope.rules.push(res);
      // $scope.currentRule = res;
      // console.log($scope.rules);
      // alert(res);
      // console.log(res);

    }, function(reason) {
      // alert(reason);
    });
  };



  $scope.goDel = function() {
    // alert('goDelete');
    $scope.rules.splice($scope.index, 1);
    // shareData.rules = $scope.rules;
    // console.log('shareData.rules====' + shareData.rules);
    $scope.isSelected = '';
    $scope.selectedItem = '';
    $scope.index = '';
    console.log($scope.rules);

  };

  $scope.goUp = function() {
    // alert('up');
    upAndDown(true);
    // shareData.rules = $scope.rules;
    // console.log('shareData.rules====' + shareData.rules)
  };

  $scope.goDown = function() {
    // alert('down');
    upAndDown(false);
    // shareData.rules = $scope.rules;
    // console.log('shareData.rules====' + shareData.rules);
  };

  //上移下移函数
  function upAndDown(up_or_down) {
    if (up_or_down) { //上移
      if ($scope.index === 0) {
        return;
      } else {
        var temp;
        for (var i = 1; i < $scope.rules.length; i++) {
          if ($scope.index == i) {
            temp = $scope.rules[i - 1];
            $scope.rules[i - 1] = $scope.rules[i];
            $scope.rules[i] = temp;
            $scope.index--;
            console.log($scope.rules);
            break;
          }
        }
      }
    } else { //下移

      if ($scope.index === $scope.rules.length - 1) {
        return;
      } else {
        var temp;
        for (var i = 0; i < $scope.rules.length - 1; i++) {
          if ($scope.index == i) {
            temp = $scope.rules[i + 1];
            $scope.rules[i + 1] = $scope.rules[i];
            $scope.rules[i] = temp;
            $scope.index++;
            console.log($scope.rules);
            break;
          }
        }
      }
    }

  }
}]);
